﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ComputerVision.Data;
using ComputerVision.Data.Repositories;

namespace ComputerVision.Services
{
    public class XeTempService : IService<XeTemp>
    {
        private readonly IEntityRepository<XeTemp> _xeTempRepository;

        public XeTempService(IEntityRepository<XeTemp> xeRepository)
        {
            _xeTempRepository = xeRepository;
        }

        public XeTemp Create(XeTemp t)
        {
            try
            {
                if (t == null)
                    throw new ArgumentNullException("entity");
                _xeTempRepository.InsertOnCommit(t);
                _xeTempRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage) + Environment.NewLine;

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
            return t;
        }

        public XeTemp Update(XeTemp t)
        {
            XeTemp item;
            try
            {
                item = _xeTempRepository.Get(t.Id);
                //item.SoVe = t.SoVe;
                //item.LoaiXeId = t.LoaiXeId;
                //item.BienSoXe = t.BienSoXe;
                //item.NgayVao = t.NgayVao;
                item.NgayRa = t.NgayRa;
                //item.GioVao = t.GioVao;
                //item.ImgVaoSau = t.ImgVaoSau;
                //item.ImgVaoTruoc = t.ImgVaoTruoc;
                item.ImgRaSau = t.ImgRaSau;
                item.ImgRaTruoc = t.ImgRaTruoc;
                item.ImgBienXeRa = t.ImgBienXeRa;
                item.CardStatus = t.CardStatus;
                //item.Status = t.Status;
                _xeTempRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += Environment.NewLine +
                               string.Format("Property: {0} Error: {1}", validationError.PropertyName,
                                   validationError.ErrorMessage);

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
            return item;
        }

        public XeTemp Detail(Guid id)
        {
            var item = _xeTempRepository.Get(id);
            return item;
        }

        public XeTemp Detail(int id)
        {
            var item = _xeTempRepository.Get(id);
            return item;
        }

        public XeTemp Detail(string id)
        {
            var item = _xeTempRepository.Get(id);
            return item;
        }

        public XeTemp GetByMaxValue(string keyField, string maxField, string table, string key)
        {
            var item = _xeTempRepository.GetByMaxValue(keyField, maxField, table, key);
            return item;
        }

        public List<XeTemp> ToList()
        {
            var item = _xeTempRepository.GetAll().ToList();
            return item;
        }

        public List<XeTemp> SqlManual(string sql)
        {
            var item = _xeTempRepository.SqlManual(sql).ToList();
            return item;
        }

        public void Delete(XeTemp t)
        {
            try
            {
                if (t == null)
                    throw new ArgumentNullException("entity");
                _xeTempRepository.DeleteOnCommit(t);
                _xeTempRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;

                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += Environment.NewLine + string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
        }
    }
}
